<!DOCTYPE html>
<%@ page language="java" import="com.compuvest.web.*,java.util.*,java.text.*,java.net.URLEncoder" pageEncoding="UTF-8" %>

<%@ taglib uri="/WEB-INF/tld/head.tld" prefix="head" %>
<%@ taglib uri="/WEB-INF/tld/extmenu.tld" prefix="menu" %>
<%@ taglib uri="/WEB-INF/tld/items.tld" prefix="itemstld" %>
<%@ taglib uri="/WEB-INF/tld/preference.tld" prefix="preference" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<fmt:setBundle basename="properties.Compuvest" var="cv" scope="page" />

<jsp:useBean id="sb" class="SQLBean" scope="application"/>
<jsp:useBean id="advertShow" class="AdvertShow" scope="application"/>
<jsp:useBean id="basket" class="Basket" scope="session"/>
<%
    String http = "./";//application.getInitParameter("http");

    SimpleDateFormat dateForm = new SimpleDateFormat("MM-dd-yyyy");
    Calendar now = Calendar.getInstance();
    Calendar working = (Calendar)now.clone();
    working.add(Calendar.DAY_OF_YEAR, +1);

    int catID = advertShow.getInt(request.getParameter("cid"), 0);
    int scatID = advertShow.getInt(request.getParameter("sid"), 0);
    int pageNo = advertShow.getInt(request.getParameter("pg"), 0);
    int sortID = advertShow.getInt(request.getParameter("srt"), 3);

    session.setAttribute("session.categoryID", Integer.toString(catID));
    session.setAttribute("session.subcatID", Integer.toString(scatID));

    String search = advertShow.getString(request.getParameter("Search"), "").replaceAll("[^a-zA-Z0-9\\-\\+\\=\\#\\.\\/\\p{Space}]+", "").trim();

    // preference
    PreferenceUtil prefUtil = new PreferenceUtil(request, scatID);

    String thisParameter = URLEncoder.encode(search.trim(), "UTF-8");

    Items searchItems = sb.getItemsSearch(catID, scatID, search.toLowerCase(), sortID, prefUtil.getContent(), pageNo);

    // no find Items redirect to instruction
    if(searchItems.isEmpty()) {
        response.sendRedirect(/*application.getInitParameter("info") +*/ "./info/SearchTips.jsp?Search=" + search);
        return;
    }
    // redirect to Description if only one item
//    else if(searchItems.size() == 1) {
//	response.sendRedirect(application.getInitParameter("http") + "Desc.jsp?Search=" + search + "&Title=" + title + "&iid=" + items[0].getItemID());
//	return;
//    }

%>
<html>
<head>
<title><fmt:message key='webcorp' bundle='${cv}'/> Search</title>
<%@include file="./incl/scripts.txt" %>
</head>
<body>

<div id='cvbody'>

 <head:show />

 <div id='cvcont'>
  <table id='cvmain'>
  <colgroup><col width='200px'/><col width='*'/><col width='160px'/></colgroup>
  <tbody>
   <tr>
    <td id='cvleft' valign="top">

<menu:extmenu
 categoryID='<%= catID %>'
 subcategoryID='<%= scatID %>'
 pageUrl='<%= http + "Search.jsp?Search=" + thisParameter + "&srt=" + sortID + "&pg=0" %>'
 style='md'
 categories='<%= searchItems.getCategories() %>'
 subcategories='<%= searchItems.getSubcategories() %>'
/>

<br/>

     <div id='leftmenu'></div>

    </td>
    <td id='cvcenter' align="center" valign="top">

<%
    // select advert coupon
    String advertImage = basket.getCouponShow(advertShow);
    out.write(advertImage);
%>

    <%-- Navigation --%>
    <div id='cv_nav'>
      &#8226; <a href='<%= http %>'>Home</a>
      &#8226; <a href='<%= http %>Search.jsp?Search=<%= thisParameter %>'><%= search %></a>
      &#8226; <b><%= searchItems.getEventTime() %></b>
    </div>

<form name='SortForm' action='<%= http %>Search.jsp' method='POST'>
<input type='hidden' name='pg' value='0'/>
<input type='hidden' name='cid' value='<%= catID %>'/>
<input type='hidden' name='sid' value='<%= scatID %>'/>
<input type='hidden' name='Search' value='<%= thisParameter %>'/>
<%= prefUtil.getHiddenTags() %>

<itemstld:show
 categoryID='<%= catID %>'
 subcategoryID='<%= scatID %>'
 pageNo='<%= pageNo %>'
 pageUrl='<%= http + "Search.jsp?Search=" + thisParameter + "&cid=" + catID + "&sid=" + scatID + prefUtil.getLink() %>'
 reseller='<%= basket.getReseller() %>'
 items='<%= searchItems %>'
 sortID='<%= sortID %>'
/>
</form>

<br/><br/>
<%
    if(searchItems.size() < 12) { // show advert items
%>
 <fieldset style='width:90%;margin:25px 0 0 0;'>
 <table class='tabyw'>
  <tr align='center'><td><font color='green'>GREAT GIFT IDEAS. OFFER EXPIRES <%= dateForm.format(working.getTime()) %></font></td></tr>
 </table>
</fieldset>

<div id='advshow'></div>

<%
    } // if
%>

    </td>
    <td id='cvright' valign="top">

     <div id='advright'></div>

<%
    if(scatID != 0) {
%>
<preference:show
 pageUrl='<%= http + "Search.jsp?Search=" + thisParameter + "&srt=" + sortID + "&cid=" + catID + "&sid=" + scatID %>'
 preferenceList='<%= searchItems.getPreference() %>'
 popUrl='<%= prefUtil.getLink() %>'
 popData='<%= prefUtil.getContent() %>'
/>
<%
    }
%>

    </td>
   </tr>
  </tbody>
  </table>
 </div>

 <div id='cvfoot'></div>
</div>


</body>
<script type="text/javascript">
  window.addEvent('domready', function() {
    new LeftMenu({data:<%= advertShow.getLeftMenu() %>});
    new AdvertShow({data:<%= advertShow.getItemsSearchPage() %>,cols:5,count:10});
  });
</script>

<!--%@include file="./incl/tracking.incl" %-->

</html>
